import { removeToken, setUserName } from "../../utils/auth";
import { RootState, IUser } from "../types";
const state: IUser = {
  username: "",
};
const mutations = {
  SET_USERNAME: (state: IUser, username: string) => {
    state.username = username;
  },
};
const actions = {
  login(context: any, userInfo: RootState) {
    return new Promise<void>((resolve, reject) => {
      context.commit("SET_USERNAME", userInfo.username);
      setUserName(userInfo.username);
    });
  },
  logout(context: any) {
    return new Promise<void>((resolve, reject) => {
      context.commit("SET_USERNAME", "");
    });
  },
  resetToken(context: any) {
    return new Promise<void>((resolve) => {
      context.commit("SET_TOKEN", "");
      removeToken();
      resolve();
    });
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
